/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.api.entityInterface;

import com.inspur.edp.bef.api.be.IBENodeEntity;
import com.inspur.edp.bef.api.be.IBusinessEntity;
import com.inspur.edp.bef.bizentity.GspBizEntityObject;
import com.inspur.edp.bef.dtgenerator.api.ApiHelper;
import com.inspur.edp.bef.dtgenerator.api.BefApiGeneratorContext;
import com.inspur.edp.bef.dtgenerator.api.entityInterface.resourceInfo.BefEntityResourceInfoGenerator;
import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.resourceInfo.EntityResourceInfoGenerator;
import com.inspur.edp.cef.generator.apiGenerator.entity.separateInterface.EntitySingleInterfaceGenerator;
import com.inspur.edp.cef.generator.overall.Utils;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import java.util.ArrayList;

public class BEEntitySingleInterfaceGenerator extends EntitySingleInterfaceGenerator {

  private BefApiGeneratorContext apiGenContext;
  private GspBizEntityObject beObject;

  public BEEntitySingleInterfaceGenerator(
      BefApiGeneratorContext apiGenContext, GspBizEntityObject beObject) {
    super(apiGenContext, beObject);
    this.apiGenContext = apiGenContext;
    this.beObject = beObject;
    if (beObject.getIsRootNode()) {
      apiGenContext
          .setRootEntityAllInterfaceType(new MediateType(apiGenContext.getNamespace(), getName()));
    }
  }

  @Override
  protected BEEntitySingleInterfaceGenerator createChildEntityGenerator(
      IGspCommonObject childObject) {
    return new BEEntitySingleInterfaceGenerator(apiGenContext, (GspBizEntityObject) childObject);
  }

  @Override
  protected String getName() {
    return ApiHelper.getBEAllInterfaceClassName(beObject);
  }

  @Override
  protected ArrayList<BaseGenerator> createEntityExtendGenerators() {
    ArrayList<BaseGenerator> list = super.createEntityExtendGenerators();
    if (list == null) {
      list = new ArrayList<>();
    }
    EntityMethodBuildUtil.addBEActions(apiGenContext, beObject, list);
    return list;
  }

//  @Override
//  protected DataTypeInfoGenerator createDataTypeInfoGenerator() {
//    return new BeEntityInfoGenerator((EntityInterfaceGenCtx) getContext());
//  }

  @Override
  protected EntityResourceInfoGenerator createResourceInfoGenerator() {
    return new BefEntityResourceInfoGenerator(getContext(), beObject);
  }

  @Override
  protected TypeInfo getDefaultInterfaceType() {
    if (beObject.getIsRootNode()) {
      return Utils.createTypeInfo(IBusinessEntity.class);
    } else {
      return Utils.createTypeInfo(IBENodeEntity.class);
    }
  }
}
